package cn.tedu.zhwy.information.service.impl;

import cn.tedu.zhwy.information.mapper.BuildingMapper;
import cn.tedu.zhwy.information.mapper.HouseMapper;
import cn.tedu.zhwy.information.pojo.dto.BuildingQuery;
import cn.tedu.zhwy.information.pojo.dto.HouseQuery;
import cn.tedu.zhwy.information.pojo.dto.HouseSaveParam;
import cn.tedu.zhwy.information.pojo.entity.House;
import cn.tedu.zhwy.information.pojo.vo.BuildingVO;
import cn.tedu.zhwy.information.pojo.vo.HouseVO;
import cn.tedu.zhwy.information.service.HouseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Slf4j
@Service
public class HouseServiceImpl implements HouseService {
    @Autowired
    private HouseMapper houseMapper;
    @Autowired
    private BuildingMapper buildingMapper;
    @Override
    public List<HouseVO> selectHouse(HouseQuery houseQuery) {
        BuildingQuery buildingQuery = new BuildingQuery();
        //判断查询条件中楼组名称是否为空或为空字符串
        if (houseQuery.getBname() != null && houseQuery.getBname() != "") {
            //把前端传过来的楼座名称放到楼座查询条件中
            buildingQuery.setBname(houseQuery.getBname());
            //通过楼座名称查出楼座id
            List<BuildingVO> buildingVOList = buildingMapper.select(buildingQuery);
            //将楼座id放到房屋查询条件中
            houseQuery.setHbid(buildingVOList.get(0).getBid());
            log.debug("查询条件:houseQuery={}",houseQuery);
        }
        List<HouseVO> list = houseMapper.select(houseQuery);
        log.debug("前房屋信息:list={}",list);
        //通过遍历将楼座名称查出并放入到VO
        for (int i = 0; i < list.size(); i++) {
            //通过先前查询到的房屋信息获取楼座id,并放到楼座查询条件中去
            buildingQuery.setBid(list.get(i).getHbid());
            //通过楼座查询条件获取相关楼座信息
            List<BuildingVO> buildingList = buildingMapper.select(buildingQuery);
            //从查询到的楼座信息集合中取第一条，并获取其楼座名称放到相关房屋信息中去
            list.get(i).setBname(buildingList.get(0).getBname());
        }
        log.debug("后房屋信息:list={}",list);
        return list;
    }

    @Override
    public void saveHouse(HouseSaveParam houseSaveParam) {
        House house = new House();
        BeanUtils.copyProperties(houseSaveParam, house);
        //通过前端传过来的数据获取到楼座名称
        String bName = house.getBname();
        if (bName != null) {
            BuildingQuery buildingQuery = new BuildingQuery();
            //把获取到的楼座名称放到楼座查询条件中
            buildingQuery.setBname(bName);
            //通过楼座查询条件获取相对应的楼座信息
            List<BuildingVO> buildingVOList = buildingMapper.select(buildingQuery);
            //通过获取到的楼座信息获取楼座id
            Long bId = buildingVOList.get(0).getBid();
            //将楼座id放到房屋条件中去
            house.setHbid(bId);
        }
        if (houseSaveParam.getHbid() != null && houseSaveParam.getHbid().toString() != "[object PointerEvent]") {
            houseMapper.update(house);
        }else {
            houseMapper.save(house);
        }

    }

    @Override
    public void deleteHouse(Long hid) {
        houseMapper.delete(hid);
    }
}
